package com.ruoyi.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.config.WebSocketManager;
import com.ruoyi.domain.WsResult;
import com.ruoyi.enums.ConnectDeviceTypeEnum;
import com.ruoyi.enums.WsCode;
import com.ruoyi.enums.WsType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/test/webSocket")
@RestController
public class TestWebSocketController {

    static WebSocketManager webSocketManager = WebSocketManager.INSTANCE;

    @GetMapping
    public AjaxResult sendMessageByUserID(Long userId){
        WsResult wsResult = new WsResult.Builder()
                .data("测试报警推送消息")
                .code(WsCode.SUCCESS.getCode())
                .type(WsType.DEVICE_WARN.getType())
                .build();

        webSocketManager.sessionSend(userId,wsResult, ConnectDeviceTypeEnum.PC);
        return AjaxResult.success("发送用户成功ID为：" + userId);
    }

    //获取当前在线的所有用户信息
    @GetMapping("/getAll")
    public AjaxResult getAllOnlinePeople(){
        return AjaxResult.success(webSocketManager.getAllOnlineUser());
    }

    //获取指定用户在线设备数量
    @GetMapping("/getUserAll")
    public AjaxResult getUserAll(Long userId){
        return AjaxResult.success(webSocketManager.size(userId));
    }

}
